<template>
  <div class="comments-container">
    <el-header>
        <span
          style="display: block; width: 100%; height: 30px; background: #001529; text-align: center; font-size: 20px; font-weight: bold; color: white">
          评论数据
        </span>
    </el-header>

    <el-card style="width: 100%">

      <el-table :data="tableData"
                style="font-weight: bold;border: #001529;"
                border
                v-loading="loading"
      >
        <el-table-column label="ID" prop="id" align="center" width="100px"></el-table-column>
        <el-table-column label="标题" prop="title" align="center" width="200px"></el-table-column>
        <el-table-column label="评论内容" prop="content" align="center"></el-table-column>
        <el-table-column label="评论时间" align="center" width="200px">
          <template slot-scope="scope">
            {{ new Date(scope.row.createTime).toLocaleString('zh-CN', {timeZone: 'Asia/Shanghai', hour12: false}) }}
          </template>
        </el-table-column>
        <el-table-column label="评论人" prop="nickname" align="center" width="100px"></el-table-column>
        <el-table-column label="操作" align="center" width="100px">
          <template slot-scope="scope">
            <div>
              <el-button size="mini" type="danger" @click="delById(scope.row)" plain>删除评论
              </el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <template>
        <div class="block">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page.sync="pageCommentsReq.pageNum"
            :page-size="pageCommentsReq.pageSize"
            layout="total, prev, pager, next"
            :total="total">
          </el-pagination>
        </div>
      </template>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      user: JSON.parse(sessionStorage.getItem("user-key" || "{}")),
      tableData: [],
      loading: false,
      pageCommentsReq: {
        pageNum: 1,
        pageSize: 3,
        createUser: '',
      },
      total: 0,

    }
  },
  mounted() {//页面加载后触发
    this.fetchData()
  },
  methods: {
    delById(row) {
      this.$request.delete('/comments/deleteById', {params: {id: row.id}}).then(res => {
        if (res.code === 200) {
          this.$message.success("删除成功")
          this.fetchData()
        } else {
          this.$message.error(res.msg)
        }
      }).catch(() => {
        this.$message.error("系统错误")
      })
    },
    fetchData() {
      this.pageCommentsReq.createUser = this.user.id
      this.$request.post('/comments/queryUserComments', this.pageCommentsReq).then(res => {
        if (res.code === 200) {
          this.loading = false
          this.tableData = res.data.comments
          this.pageCommentsReq.pageNum = res.data.pageNum
          this.pageCommentsReq.pageSize = res.data.pageSize
          this.total = res.data.total
        }
      }).catch(() => {
        this.loading = false;
        this.$message.error("系统错误")
      })
    },
    handleSizeChange(val) {
      this.pageCommentsReq.pageSize = val;
      this.fetchData(); // 当每页显示条数改变时，重新获取数据
    },
    handleCurrentChange(val) {
      this.pageCommentsReq.pageNum = val;
      this.fetchData(); // 当每页显示条数改变时，重新获取数据
    },
  }
}
</script>

<style scoped>

</style>
